package com.hnucm.springboot.springboot.controller;

import com.hnucm.springboot.springboot.model.Person;
import com.hnucm.springboot.springboot.model.StuCard;
import com.hnucm.springboot.springboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/personpage")
    public String getAllPerson(Model model){
        model.addAttribute("personList",personService.getAllPerson());
        return "personpage.html";
    }
    //转发到添加页面
    @RequestMapping("/addpersonpage")
    public String addPerson(){
        return "addpersonpage.html";
    }
    //提交请求
    @RequestMapping("/addpersoncommit")
    public String addPersonCommit(String name, int age,String stuid,String classname){
        Person person = new Person();
        person.setAge(age);
        person.setName(name);
        StuCard stuCard = new StuCard();
        stuCard.setClassname(classname);
        stuCard.setStuid(stuid);
        person.setStuCard(stuCard);
        personService.addPerson(person);
        return "redirect:/personpage";
    }
    @RequestMapping("deleteperson")
    public String deletePerson(int id,int stuid){
        personService.deletePerson(id,stuid);
        return "redirect:/personpage";
    }
    @RequestMapping("updatepersonpage")
    public String updatepersonpage(int id,String name,int age,int stucardid,String stuid,String classname,
                                   Model model){
        model.addAttribute("id",id);
        model.addAttribute("age",age);
        model.addAttribute("stucardid",stucardid);
        model.addAttribute("name",name);
        model.addAttribute("stuid",stuid);
        model.addAttribute("classname",classname);
        return "update.html";
    }

    @RequestMapping("updatepersoncommit")
    public String updatepersoncommit(int id,String name,int age,int stucardid,String stuid,String classname){
        Person person = new Person();
        person.setId(id);
        person.setAge(age);
        person.setName(name);
        StuCard stuCard = new StuCard();
        stuCard.setId(stucardid);
        stuCard.setClassname(classname);
        stuCard.setStuid(stuid);
        person.setStuCard(stuCard);
        personService.updatePerson(person);
        return "redirect:/personpage";
    }

}
